package com.api.gbhg.liubing.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liubing.dao.EndowmentInsuranceDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : liubing
 * @date :  2021/1/8 14:47
 * 养老保险列表
 */
public class EndowmentInsuranceAction {
    public static final Logger LOGGER= LoggerFactory.getLogger(PropertyManagementAction.class);
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    // 养老保险列表
    @POST
    @Path("/EndowmentInsuranceAction")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getEndowmentInsuranceAction(Map<String,String> param) {
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
        EndowmentInsuranceDao dao = new EndowmentInsuranceDao();
        String requestId=param.get("requestId");
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
//        int total = dao.getMainTableCount(status);//获取数据总条数
//        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);
        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()) {
            map[i] = new HashMap<>();
            map[i].put("lcid", mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin", mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw", sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks", sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if (loginid == "") {
                loginid = "系统管理员";
            }
            map[i].put("lcjlsqjlid", mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr", loginid);  //编报人
            BigDecimal hwgz=new BigDecimal("0.00");
            BigDecimal gc=new BigDecimal("0.00");
            BigDecimal fw=new BigDecimal("0.00");
            BigDecimal xecg=new BigDecimal("0.00");
            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("zfcgys",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            if (!mainTable.getString("yszje").equals("")){
                map[i].put("yszje",mainTable.getString("yszje"));//年预算
            }else {
                map[i].put("yszje","0.00");//年预算
            }
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            int zzrygzffrs =0;//在职人员工资发放人数
            BigDecimal dwyjnjbylj=new BigDecimal("0.00");//单位应缴纳基本养老金
            BigDecimal dwyjnzynj=new BigDecimal("0.00");//单位应缴纳职业年金
            while (rs.next()){
                if (!(rs.getInt("zzrygzffrs")==-1)){
                    zzrygzffrs=zzrygzffrs+rs.getInt("zzrygzffrs");
                }
                if (!(rs.getInt("dwyjnjbylj")==-1)){
                    dwyjnjbylj=dwyjnjbylj.add(new BigDecimal(rs.getInt("dwyjnjbylj")));
                }
                if (!(rs.getInt("dwyjnzynj")==-1)){
                    dwyjnzynj=dwyjnzynj.add(new BigDecimal(rs.getInt("dwyjnzynj")));
                }
            }
            map[i].put("zzrygzffrs", zzrygzffrs);
            map[i].put("dwyjnjbylj", dwyjnjbylj.toString());
            map[i].put("dwyjnzynj",dwyjnzynj.toString());
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_hgylbxjfjfxqb");
        return  JSON.toJSONString(page);
    }

    // 15 海关养老保险缴费经费需求表
    public String getEndowmentInsuranceAction1(String requestId) {
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
        EndowmentInsuranceDao dao = new EndowmentInsuranceDao();
//        String requestId=param.get("requestId");
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
//        int total = dao.getMainTableCount(status);//获取数据总条数
//        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);
        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()) {
            map[i] = new HashMap<>();
            map[i].put("lcid", mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin", mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw", sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks", sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if (loginid == "") {
                loginid = "系统管理员";
            }
            map[i].put("lcjlsqjlid", mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr", loginid);  //编报人
            BigDecimal wan = new BigDecimal("10000");
            BigDecimal hwgz=new BigDecimal("0.0000");
            BigDecimal gc=new BigDecimal("0.0000");
            BigDecimal fw=new BigDecimal("0.0000");
            BigDecimal xecg=new BigDecimal("0.0000");
            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            map[i].put("zfcgys",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            if (!mainTable.getString("yszje").equals("")){
                map[i].put("yszje",mainTable.getString("yszje"));//年预算
            }else {
                map[i].put("yszje","0.00");//年预算
            }
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            int zzrygzffrs =0;//在职人员工资发放人数
            BigDecimal dwyjnjbylj=new BigDecimal("0.0000");//单位应缴纳基本养老金
            BigDecimal dwyjnzynj=new BigDecimal("0.0000");//单位应缴纳职业年金
            while (rs.next()){
                if (!(rs.getInt("zzrygzffrs")==-1)){
                    zzrygzffrs=zzrygzffrs+rs.getInt("zzrygzffrs");
                }
                if (!(rs.getInt("dwyjnjbylj")==-1)){
                    dwyjnjbylj=dwyjnjbylj.add(new BigDecimal(rs.getInt("dwyjnjbylj")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
                }
                if (!(rs.getInt("dwyjnzynj")==-1)){
                    dwyjnzynj=dwyjnzynj.add(new BigDecimal(rs.getInt("dwyjnzynj")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
                }
            }
            map[i].put("zzrygzffrs", zzrygzffrs);
            map[i].put("dwyjnjbylj", dwyjnjbylj.toString());
            map[i].put("dwyjnzynj",dwyjnzynj.toString());
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_hgylbxjfjfxqb");
        return  JSON.toJSONString(page);
    }
}
